//setup for Ajax
function newXMLHttpRequest() {
    var xmlreq = false;
    if (window.XMLHttpRequest) {
        xmlreq = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        // Try ActiveX
        try { 
            xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e1) { 
            // first method failed 
            try {
                xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e2) {
                // both methods failed 
                alert("Trinh duyet khong ho tro Ajax. Xin su dung trinh duyet khac.");
            } 
        }
    }
    return xmlreq;
} 

//Khoi tao doi tuong http
var http = newXMLHttpRequest();
function getReadyStateHandler(http, responseXmlHandler) {
    return function () {
        if (http.readyState == 4) {
            if (http.status == 200) {
                responseXmlHandler(http.responseXML);
            } else {
                alert("Het thoi gian thuc thi. Xin thu lai mot lan nua.");
            }
        }
    }
}
//
function message_success(mess,id){
    if(document.getElementById(id)!=undefined){
        document.getElementById(id).innerHTML = mess;
        document.getElementById(id).style.color = "green";
    }
}
function message_error(mess,id){
    if(document.getElementById(id)!=undefined){
        document.getElementById(id).innerHTML = mess;
        document.getElementById(id).style.color = "red";
    }
}
//hien thi thong bao khi ajax thuc thi
function ajaxConnectCheckDuyetGV(duyet,mahknk,manhom,tuan,thu,tietbatdau) {
    if(duyet == true) duyet = 1;
    else duyet = 0;
    var callbackHandler = getReadyStateHandler(http,ajaxResultCheckDuyetGV);
    var params = "task=checkduyet&mahknk="+mahknk+"&manhom="+manhom+"&tuan="+tuan+"&thu="+thu+"&tietbatdau="+tietbatdau+"&duyet="+duyet;
    http.open("POST","ajax_server.php",true);
    http.onreadystatechange = callbackHandler;
    http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    http.send(params);
//    alert(params);
}
function ajaxResultCheckDuyetGV(rXML) {
    var mahknk = rXML.getElementsByTagName("hknk")[0].firstChild.nodeValue;
    var tietbatdau = rXML.getElementsByTagName("tietbatdau")[0].firstChild.nodeValue;
    var thu = rXML.getElementsByTagName("thu")[0].firstChild.nodeValue;
    var tuan = rXML.getElementsByTagName("tuan")[0].firstChild.nodeValue;
    var manhom = rXML.getElementsByTagName("manhom")[0].firstChild.nodeValue;
    var result = rXML.getElementsByTagName("result")[0].firstChild.nodeValue;
    var val = rXML.getElementsByTagName("duyet")[0].firstChild.nodeValue;
       
    ajaxConnectCheckTrangThai(mahknk,manhom,tuan,thu,tietbatdau);
    if(result=="Fail"){
        if(val==1){
            document.getElementById('txtCheck_Id').checked = false;
        }else{
            document.getElementById('txtCheck_Id').checked = true;
        }
        message_error('Duy\u1ec7t thất bại!!! Vui lòng thử lại!','divmess');
    }else{
        message_success('Duy\u1ec7t thành công!','divmess');      
    }
    document.getElementById('duyetgvid_'+manhom+tuan+thu+tietbatdau).value = val;
}
function ajaxConnectCheckTrangThai(mahknk,manhom,tuan,thu,tietbatdau) {
    var callbackHandler = getReadyStateHandler(http,ajaxResultCheckTrangThai);
    var params = "task=checktrangthai&mahknk="+mahknk+"&manhom="+manhom+"&tuan="+tuan+"&thu="+thu+"&tietbatdau="+tietbatdau;
    http.open("POST","ajax_server.php",true);
    http.onreadystatechange = callbackHandler;
    http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    http.send(params);
//    alert(params);
}
function ajaxResultCheckTrangThai(rXML) {
    var tietbatdau = rXML.getElementsByTagName("tietbatdau")[0].firstChild.nodeValue;
    var thu = rXML.getElementsByTagName("thu")[0].firstChild.nodeValue;
    var tuan = rXML.getElementsByTagName("tuan")[0].firstChild.nodeValue;
    var manhom = rXML.getElementsByTagName("manhom")[0].firstChild.nodeValue;
    var result = rXML.getElementsByTagName("result")[0].firstChild.nodeValue;
    document.getElementById('TrangThaiId_'+manhom+tuan+thu+tietbatdau).innerHTML = '';
    if(result==1){
        document.getElementById('TrangThaiId_'+manhom+tuan+thu+tietbatdau).innerHTML = "<b style='color:green'>\u0110ã duyệt</b>";
    }else{
        document.getElementById('TrangThaiId_'+manhom+tuan+thu+tietbatdau).innerHTML = "<b style='color:red'>Ch\u01b0a duyệt</b>";
    }   
}
//hien thi thong bao khi ajax thuc thi
function ajaxConnectCheckDuyetSV(duyet,mahknk,manhom,tuan,thu,tietbatdau) {
    if(duyet == true) duyet = 1;
    else duyet = 0;
    var callbackHandler = getReadyStateHandler(http,ajaxResultCheckDuyetSV);
    var params = "task=checkduyetsv&mahknk="+mahknk+"&manhom="+manhom+"&tuan="+tuan+"&thu="+thu+"&tietbatdau="+tietbatdau+"&duyet="+duyet;
    http.open("POST","ajax_server.php",true);
    http.onreadystatechange = callbackHandler;
    http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    http.send(params);
    //alert(params);
}
function ajaxResultCheckDuyetSV(rXML) {
    
    var mahknk = rXML.getElementsByTagName("hknk")[0].firstChild.nodeValue;
    var tietbatdau = rXML.getElementsByTagName("tietbatdau")[0].firstChild.nodeValue;
    var thu = rXML.getElementsByTagName("thu")[0].firstChild.nodeValue;
    var tuan = rXML.getElementsByTagName("tuan")[0].firstChild.nodeValue;
    var manhom = rXML.getElementsByTagName("manhom")[0].firstChild.nodeValue;
    var result = rXML.getElementsByTagName("result")[0].firstChild.nodeValue;
    var val = rXML.getElementsByTagName("duyet")[0].firstChild.nodeValue;
       
    ajaxConnectCheckTrangThai(mahknk,manhom,tuan,thu,tietbatdau);
    if(result=="Fail"){
        if(val==1){
            document.getElementById('txtCheck_Id_1').checked = false;
        }else{
            document.getElementById('txtCheck_Id_1').checked = true;
        }
        message_error('Duy\u1ec7t thất bại!!! Vui lòng thử lại!','divmess_1');
    }else{
        message_success('Duy\u1ec7t thành công!','divmess_1');      
    }
    document.getElementById('duyetsvid_'+manhom+tuan+thu+tietbatdau).value = val;
    
}
//check Hien thi thong bao
function ajaxConnectCheckHienThiTB(masotb, hienthi) {
    if(hienthi == true) hienthi = 0;
    else hienthi = 1;
    var callbackHandler = getReadyStateHandler(http,ajaxResultCheckHienThiTB);
    var params = "task=checkhienthitb&masotb="+masotb+"&hienthi="+hienthi;
    http.open("POST","ajax_server.php",true);
    http.onreadystatechange = callbackHandler;
    http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    http.send(params);
    //alert(params);
}
function ajaxResultCheckHienThiTB(rXML) {
   // alert('params');
   
    var result = rXML.getElementsByTagName("result")[0].firstChild.nodeValue;
    var masotb = rXML.getElementsByTagName("masotb")[0].firstChild.nodeValue;
    var hienthi = rXML.getElementsByTagName("hienthi")[0].firstChild.nodeValue;
    if(result=="Fail"){
        if(hienthi==1)
            document.getElementById('checkHienThiId_'+masotb).checked = false;
        else document.getElementById('checkHienThiId_'+masotb).checked = true;
    }
}